//
//  LXTableViewController.m
//  CarTown
//
//  Created by 熊浩 on 15/8/3.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "LXTableViewController.h"
#import "CurrtPoint.h"
#import "NumModel.h"
#import <MJExtension.h>
@interface LXTableViewController ()

@property (nonatomic,assign) NSInteger oldpostion;
@property (nonatomic,assign) NSInteger oldsection;

@end

@implementation LXTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currtpostion=0;
    self.currtkuwei=[[CurrtPoint alloc]init];
    
    self.currtlexing=[[CurrtPoint alloc]init];
    self.currtneishi=[[CurrtPoint alloc]init];
    self.currtwaiguan=[[CurrtPoint alloc]init];
    self.currtxingzhi=[[CurrtPoint alloc]init];
    self.currtzhuangtai=[[CurrtPoint alloc]init];
    self.currtkuwei=[[CurrtPoint alloc]init];
    
  
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0;
    }else{
        return 44;
    }
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return @"";
    }else if(section==1){
        return self.array[1];
    }else{
        return self.array[2];
    }
}
-(void)setArray:(NSMutableArray *)array{
    _array=array;
    switch (self.Choice) {
        case LEIXING:
           self.currtpostion= self.currtlexing.currtpostion;
          self.currtsection= self.currtlexing.currtsection;
            break;
        case KUWEI:
          self.currtpostion=self.currtkuwei.currtpostion;
           self.currtsection= self.currtkuwei.currtsection;
            break;
        case ZHUANGTAI:
           self.currtpostion= self.currtzhuangtai.currtpostion;
           self.currtsection= self.currtzhuangtai.currtsection;
            break;
        case WAIGUAN:
           self.currtpostion= self.currtwaiguan.currtpostion;
           self.currtsection=self.currtwaiguan.currtsection;
            break;
        case XINGZHI:
          self.currtpostion=self.currtxingzhi.currtpostion;
           self.currtsection= self.currtxingzhi.currtsection;
            break;
        case NEISHI:
           self.currtpostion= self.currtneishi.currtpostion;
           self.currtsection= self.currtneishi.currtsection;
            break;
        default:
            break;
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return self.ischeyuan?3:1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (self.ischeyuan) {
        if (section==0) {
            return 1;
        }else if(section==1){
            return 1;
        }else{
            return _array.count-2;
        }
    }else{
    return self.array.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mark" forIndexPath:indexPath];
    cell.textLabel.font=[UIFont systemFontOfSize:14];
    if (indexPath.row==self.currtpostion&&indexPath.section==self.currtsection) {
        cell.accessoryType= UITableViewCellAccessoryCheckmark;
        cell.textLabel.textColor=lightblue;
        self.oldpostion=indexPath.row;
        self.oldsection=indexPath.section;
    }else{
        cell.accessoryType= UITableViewCellAccessoryNone;
         cell.textLabel.textColor=textnormalGary;
    }
    // Configure the cell...
    if (self.ischeyuan) {
        if (indexPath.section==0) {
            cell.textLabel.text=self.array[0];
        }else if(indexPath.section==1){
            cell.textLabel.text=self.array[1];
        }else{
            cell.textLabel.text=self.array[indexPath.row+2];
        }
        
    }else{
    cell.textLabel.text=self.array[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = bgGarycolor;
    
    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:textnormalGary];
    header.textLabel.font=[UIFont systemFontOfSize:14];
    
    // Another way to set the background color
    // Note: does not preserve gradient effect of original header
    // header.contentView.backgroundColor = [UIColor blackColor];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    self.currtpostion=indexPath.row;
    self.currtsection=indexPath.section;
    NSIndexPath *old=[NSIndexPath indexPathForRow:self.oldpostion inSection:self.oldsection];
    NSIndexPath *new=[NSIndexPath indexPathForRow:self.currtpostion inSection:self.currtsection];
    [self.tableView reloadRowsAtIndexPaths:@[old,new] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    switch (self.Choice) {
        case LEIXING:
            self.currtlexing.currtpostion=self.currtpostion;
             self.currtlexing.currtsection=self.currtsection;
            break;
        case KUWEI:
            self.currtkuwei.currtpostion=self.currtpostion;
            self.currtkuwei.currtsection=self.currtsection;
            break;
        case ZHUANGTAI:
           self.currtzhuangtai.currtpostion=self.currtpostion;
            self.currtzhuangtai.currtsection=self.currtsection;
            break;
        case WAIGUAN:
           self.currtwaiguan.currtpostion= self.currtpostion;
            self.currtwaiguan.currtsection=self.currtsection;
            break;
        case XINGZHI:
          self.currtxingzhi.currtpostion=self.currtpostion;
            self.currtxingzhi.currtsection=self.currtsection;
            break;
        case NEISHI:
           self.currtneishi.currtpostion=self.currtpostion;
            self.currtneishi.currtsection=self.currtsection;
            break;
        default:
            break;
    }
    if ([_delegate respondsToSelector:@selector(didselected:AndIndexPath:WithLxTableChoice:)]) {
        NSString *str;
        if (self.ischeyuan) {
            if (indexPath.section==0) {
                str=self.array[0];
            }else if(indexPath.section==1){
                str=self.array[1];
            }else{
                str=self.array[indexPath.row+2];
            }
        }else{
            str=self.array[indexPath.row];
        }
        [_delegate didselected:str AndIndexPath:indexPath.row WithLxTableChoice:self.Choice];
    }
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
